<template>
  <view class="uni-pa-4 bg-white">
    <view>
      <uni-easyinput class="uni-my-6" prefixIcon="phone" v-model="account.phone" placeholder="請輸入手機號"
        @iconClick="iconClick"></uni-easyinput>
      <uni-easyinput class="uni-my-6" type="password" v-model="account.password" placeholder="請輸入密碼"></uni-easyinput>
      <button @click="login" class="uni-my-6">登錄</button>
      <button @click="goto('/pages/login/register')">注冊</button>
    </view>
  </view>
</template>

<script>
import {
  httpUserLogin
} from '@/utils/api.js'
export default {
  data() {
    return {
      account: {
        phone: '13129089610',
        password: '123456'
      }
    }
  },
  onLoad() {
  },
  methods: {
    async login() {
      const { code, data, message } = await httpUserLogin(this.account.phone, this.account.password)
      if (code === 200) {
        uni.setStorage({
          key: 'user_key',
          data: data.user,
          success: () => {
            uni.showToast({
              title: '登錄成功',
              icon: 'none'
            })
            uni.$emit('getUserByApp')
            uni.reLaunch({
              url: '/pages/index/index'
            })
          }
        })
      } else {
        uni.showToast({
          title: message,
          icon: 'none'
        })
      }
    },
    goto(url) {
      uni.navigateTo({
        url
      });
    },
  }
}
</script>

<style lang="scss" scoped></style>